import {requestAtom} from "../utils/request";
import FeedAnalyzer from "../utils/analyzer";
import {AxiosResponse} from "axios";

export default async function BookCrawler(url: string, lang: string) {
    try {
        const result = await requestAtom(url, lang)
        const analyzer = new FeedAnalyzer()
        await analyzer.analyze(result.data)
        const allBooks = await queryAllLang(analyzer.total, analyzer.size, url, lang)
        for (let i = 0; i < allBooks.length; i++) {
            await analyzer.analyze(allBooks[i])
        }
        return analyzer.bookArr
    } catch (e) {
        console.log(e)
        throw e
    }
}

function queryAllLang(total: number, size: number, url: string, lang: string) {
    const pages = Math.ceil(total / size)
    let ajaxs: Promise<AxiosResponse>[] = []
    if (pages > 1) {
        for (let i = 2; i <= pages; i++) {
            ajaxs.push(requestAtom(url, lang, i).then(res => {
                return res
            }))
        }
        return Promise.all(ajaxs).then(res => {
            return [...res.map(item => {
                return item.data
            })]
        }).catch(() => {
            console.log(lang +'报错')
            return []
        })
    } else {
        return Promise.resolve([])
    }
}
